//
//  LZMineViewController.m
//  laziz_Consumer
//
//  Created by XMac on 16/10/24.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMineViewController.h"
#import "LZSettingsViewController.h"

#import "LZMyPersonInfoViewController.h"
#import "LZLoginViewController.h"
#import "LZMyCouponsViewController.h"

#import "LZMyCollectionView.h"
#import "TShareView.h"

#import "Masonry.h"
#import "UIViewController+NavigationItemSetting.h"

#import "LZUserModel.h"

#import "LZMyRecordsViewController.h"
#import "TShareEventHandler.h"
#import "LZUserInfoPresenter.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "LZRemindView.h"
#import "LZMyMessageCenterViewController.h"
#import "AppDelegate.h"

extern NSString *customer;

@interface LZMineViewController ()

@property (nonatomic, strong) LZMyCollectionView * collectionView;
@property (nonatomic, strong) TShareView * shareView;

@property (assign, nonatomic) CGFloat lzAlpha;
@property (nonatomic, strong) LZUserInfoPresenter * userInfoPresenter;

@end

@implementation LZMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.umLogPageViewName = @"个人中心页面";
    
    self.view.backgroundColor = UIColorFromRGB(0xffffff, 1);
    self.userInfoPresenter = [[LZUserInfoPresenter alloc] init];
    WS(weakSelf);
    
    [self.view addSubview:self.collectionView];
    self.collectionView.didSelectRowBlock = ^(NSIndexPath * indexPath) {
        
        switch (indexPath.row) {
            case 0: //我的收藏
            {
                if ([[LZUserModel Instance] isLogin]) {
                    //跳转我的收藏
                    LZMyRecordsViewController *vc = [[LZMyRecordsViewController alloc] init];
                    vc.vcType = MyRecordsType_Favorites;
                    vc.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }
                else {
                    LZLoginViewController * vc = [[LZLoginViewController alloc] init];
                    vc.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }
            }
                break;
            case 1: //消息中心
            {
                if ([[LZUserModel Instance] isLogin]) {
                    LZMyMessageCenterViewController *settingsVC = [[LZMyMessageCenterViewController alloc]initWithNibName:@"LZMyMessageCenterViewController" bundle:nil];
                    settingsVC.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:settingsVC animated:YES];                }
                else {
                    LZLoginViewController * vc = [[LZLoginViewController alloc] init];
                    vc.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }
            }
                break;
            case 2: //设置
            {
                LZSettingsViewController *settingsVC = [[LZSettingsViewController alloc]initWithNibName:@"LZSettingsViewController" bundle:nil];
                settingsVC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:settingsVC animated:YES];
            }
                break;
            case 3: //评价app
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=1156718120&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8"]];
            }
                break;
            case 4: //分享app
            {
                [weakSelf.shareView shareViewOpen];
                weakSelf.shareView.shareViewButtonActionBlock = ^(TSHAREVIEW_BUTTON_TYPE type){
                    [weakSelf shareAction:type];
                };
            }
                break;
            case 5: //商户app
            {
                [TShareEventHandler jumpToAppstoreForMerchantApp];
            }
                break;
                
            default:
                break;
        }
    };
    
    self.collectionView.myHeadViewButtonActionBlock = ^(LZ_MY_HEAD_BUTTON_TAG tag) {
        
        if ([[LZUserModel Instance] isLogin]) {
            
            if (tag == LZ_MY_HEAD_BUTTON_TAG_COUPONS) {         //我的优惠劵
                LZMyCouponsViewController * vc = [[LZMyCouponsViewController alloc] init];
                vc.title = LZLocalizedString(@"mine_coupons", nil);
                vc.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
            else if (tag == LZ_MY_HEAD_BUTTON_TAG_TOBEREVIEW) { //未评论列表
                LZMyRecordsViewController *vc = [[LZMyRecordsViewController alloc] init];
                vc.vcType = MyRecordsType_ToBeReviewed;
                vc.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
            else if (tag == LZ_MY_HEAD_BUTTON_TAG_REVIEW){      //已评论列表
                LZMyRecordsViewController *vc = [[LZMyRecordsViewController alloc] init];
                vc.vcType = MyRecordsType_Reviewed;
                vc.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
            else if (tag == LZ_MY_HEAD_BUTTON_TAG_LOGIN) {
                LZMyPersonInfoViewController * vc = [[LZMyPersonInfoViewController alloc] init];
                vc.hidesBottomBarWhenPushed = YES;
                vc.LogoutActionBlock = ^(){
                    weakSelf.collectionView.messageHaveUnRead = NO;
                    [weakSelf.collectionView reloadData];
                };
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        }
        else {
            LZLoginViewController * vc = [[LZLoginViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
    };
    
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
    
    [self getUserCenterInfo];
    
}

#pragma mark - get info
- (void)getUserCenterInfo
{
    if ([LZUserModel Instance].isLogin) {
        WS(weakSelf);
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            [weakSelf.userInfoPresenter userGetUserInfoBlockonSuccess:^(id data) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf.collectionView updateHeaderView];
                    BOOL aa = [data[@"data"][@"messageUnRead"] boolValue];
                    weakSelf.collectionView.messageHaveUnRead = aa;
                    [weakSelf.collectionView reloadData];
                    AppDelegate *d = (AppDelegate *)[UIApplication sharedApplication].delegate;
                    if (aa == NO) {
                        [d hiddenTabbarBadge];
                    }else{
                        [d showTabbarBadge];
                    }
                });
            } onFail:^(NSString *fail) {
                DLog(@"error: %@", fail);
            }];
        });
    } else {
        [self.collectionView updateHeaderView];
    }

}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

- (BOOL)fd_prefersNavigationBarHidden {
    return YES;
}

- (void)shareAction:(NSInteger)type {
    /* 分享APP */
    SSDKPlatformType sharePlatformType = SSDKPlatformTypeUnknown;
    NSArray * imageArray;
    
    switch (type) {
        case 0:
            sharePlatformType = SSDKPlatformTypeSMS;
            break;
        case 1:
            sharePlatformType = SSDKPlatformTypeMail;
            break;
        case 2:
        {
            sharePlatformType = SSDKPlatformTypeFacebook;
            
            NSString *imageName = @"default/laziz_en.png";
            if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
                imageName = @"default/laziz_ar.png";
            }
            
            imageArray = @[[NSString stringWithFormat:@"%@%@", APIBaseImageUrl, imageName]];
        }
            break;
        case 3:
        {
            sharePlatformType = SSDKPlatformTypeTwitter;
            
            NSString *imageName = @"share_app_image_en";
            if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
                imageName = @"share_app_image_ar";
            }
            
            imageArray = @[[UIImage imageNamed:imageName]];
        }
            
            break;
    }
    
    [TShareEventHandler shareWithShareURL:[NSString stringWithFormat:@"%@%@%@", APIBaseURL, customer, @"/dist/r.html"]
                            shareImageUrl:imageArray
                               shareTitle:LZLocalizedString(@"share_app_title", nil)
                                shareText:LZLocalizedString(@"share_app_content", nil)
                                shareType:sharePlatformType
                                  topView:nil
                               stateBlock:^(SSDKResponseState state) {
                                   if (state == SSDKResponseStateSuccess) {
                                       [LZRemindView initWithImage:[UIImage imageNamed:@"提示对勾"] andContent:LZLocalizedString(@"share_success", nil)];
                                   }
                                   else if (state == SSDKResponseStateFail) {
                                       [LZRemindView initWithImage:[UIImage imageNamed:@"error_image"] andContent:LZLocalizedString(@"share_fail", nil)];
                                   }
                                   else if (state == SSDKResponseStateCancel) {
                                       [LZRemindView initWithImage:[UIImage imageNamed:@"error_image"] andContent:LZLocalizedString(@"share_fail", nil)];
                                   }
                               }];
    
    [self.shareView shareViewClose];
}

- (UICollectionView *)collectionView {
    
    if (_collectionView == nil) {
        
        GET_SCREEN_WIDTH(screen_width);
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(screen_width/2.0, 100);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(10.f, 0, 50.f, 0);
        layout.headerReferenceSize = CGSizeMake(screen_width, 290);
        
        _collectionView = [[LZMyCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout];
        
        _collectionView.iconArray = @[@"lz_my_collection_icon",
                                      @"mine_msgcenter",
                                      @"lz_my_setting_icon",
                                      @"lz_my_evaluationapp_icon",
                                      @"lz_my_shareapp_icon",
                                      @"lz_my_merchants_icon"];
        
        _collectionView.titleArray = @[LZLocalizedString(@"mine_my_favorites", @""),
                                       LZLocalizedString(@"message_message_center", @""),
                                       LZLocalizedString(@"mine_setting",@""),
                                       LZLocalizedString(@"mine_rateapp",@""),
                                       LZLocalizedString(@"mine_shareapp",@""),
                                       LZLocalizedString(@"mine_restaurants",@"")];
    }
    
    return _collectionView;
}

- (TShareView *)shareView {
    
    if (_shareView == nil) {
        _shareView = [[TShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    }
    
    return _shareView;
}

@end
